package com.sevenGroup.pawnshop.handle;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sevenGroup.pawnshop.Utils.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class failHandler implements AuthenticationFailureHandler {
    ObjectMapper om=new ObjectMapper();
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        Result success = Result.fail(null);
        success.setState(403);
        success.setMessage(e.getMessage());
        httpServletResponse.setCharacterEncoding("utf-8");
        httpServletResponse.setContentType("application/json;charset=utf-8");
        String string = om.writeValueAsString(success);
        httpServletResponse.getWriter().write(string);
        httpServletResponse.getWriter().flush();
    }
}
